package com.shop.web.sales;

import com.shop.annotation.MyLog;
import com.shop.entity.SalesUser;
import com.shop.service.SalesUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    private SalesUserService salesUserService;

    /**
     * 访问首页
     *
     * @return
     */
    @RequestMapping("/toIndex.html")
    public String toIndex() {
        return "sales/index";
    }

    /**
     * 访问登录页面
     *
     * @return
     */
    @RequestMapping("/toLogin.html")
    public String toLogin() {
        return "sales/login";
    }

    /**
     * 登录请求
     *
     * @param username
     * @param password
     */
    @RequestMapping( value = "/login.do")
    @MyLog(title = "销售模块", content = "销售人员登录")
    public void login(String username, String password, HttpServletRequest request, HttpServletResponse response) throws IOException {
        SalesUser salesUser = salesUserService.checkLogin(request, username, password);
        request.getSession().setAttribute("salesname", salesUser.getUsername());
        response.sendRedirect("/mall/sales/toIndex.html");
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/logout.do")
    @MyLog(title = "销售模块", content = "销售人员登出")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("sales_login_user");
        request.getSession().removeAttribute("salesname");
        response.sendRedirect("toLogin.html");
    }
}
